package com.achen.service.impl;

import com.achen.dao.CheckGroupDao;
import com.achen.entity.PageResult;
import com.achen.entity.QueryPageBean;
import com.achen.pojo.CheckGroup;
import com.achen.service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 * @author achen
 * @create 2020-04-08 16:29
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService{

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void addCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        //添加检查组
        checkGroupDao.addCheckGroup(checkGroup);
        //获取检查组id(通过selectKey)
        Integer checkGroupId = checkGroup.getId();
        //添加检查组和检查项的关联表(多对多)
        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage(); //当前页
        Integer pageSize = queryPageBean.getPageSize(); //每页显示几条记录
        String queryString = queryPageBean.getQueryString(); //插叙条件

        //pageHelper分页
        PageHelper.startPage(currentPage,pageSize);

        //根据条件查询
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        long total = page.getTotal(); //总页数
        List<CheckGroup> rows = page.getResult(); //根据条件查询的数据
        return new PageResult(total,rows);
    }

    @Override
    public CheckGroup findCheckGroupById(Integer id) {
        return checkGroupDao.findCheckGroupById(id);
    }

    @Override
    public List<Integer> findCheckItemByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemByCheckGroupId(id);
    }

    @Override
    public void editCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改检查组基本信息
        checkGroupDao.editCheckGroup(checkGroup);
        //获取检查组id(通过selectKey)
        Integer checkGroupId = checkGroup.getId();
        //删除关联表中相关联的信息
        checkGroupDao.delAssociationByCheckGroupId(checkGroupId);
        //重新建立关联表中相关联的信息(多对多)
        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }

    @Override
    public List<CheckGroup> findAllCheckGroup() {
        return checkGroupDao.findAllCheckGroup();
    }

    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds){
        //添加检查组和检查项的关联表(多对多)
        Map<String, Integer> map = null;
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds){
                map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                System.out.println("111");
                //添加到t_checkgroup_checkitem
                checkGroupDao.addCheckItemAndCheckGroup(map);
            }
        }
    }
}
